import QtQuick
import Qt5Compat.GraphicalEffects
import Fk
import Fk.Widgets as W

Item {
  id: root
  height: 50
  width: img.width

  property bool hovered: false
  property bool enabled: true
  property alias text: imgText.text
  
  signal clicked
  signal rightClicked

  BorderImage {
    id: img
    height: root.height
    width: imgText.contentWidth + 12
    border.left: height > 40 ? 14 : 7
    border.top: height > 40 ? 14 : 7
    border.right: height > 40 ? 14 : 7
    border.bottom: height > 40 ? 14 : 7
    source: Cpp.path + "/packages/lunarltk-qsgs-ui/image/system/selectImageButton" + (height > 40 ? "" : "Small") + ".png"
  }

  Text {
    id: imgText
    text: "按钮"
    color: '#eee9d4'
    font.pixelSize: Math.round(height / 1.4)
    font.letterSpacing: Math.round(height / 1.4) / 8
    font.bold: true
    anchors.verticalCenter: parent.verticalCenter
    // style: Text.Outline
    // styleColor: "black"
  }

  LinearGradient  {
    anchors.fill: imgText
    source: imgText
    gradient: Gradient {
      GradientStop {
        position: 0
        color: '#ece0cd'
      }

      GradientStop {
        position: 0.4
        color: '#ecc19e'
      }

      GradientStop {
        position: 0.6
        color: '#976e39'
      }

      GradientStop {
        position: 1
        color: '#77481b'
      }
    }
  }

  HoverHandler {
    id: hover
    cursorShape: root.enabled ? Qt.PointingHandCursor : Qt.ArrowCursor
    onHoveredChanged: {
      if (hovered) {
        root.hovered = true;
      } else {
        root.hovered = false;
      }
    }
  }

  Component.onCompleted: {
    imgText.anchors.horizontalCenter = horizontalCenter;
  }

  BrightnessContrast {
    anchors.fill: parent
    source: parent
    brightness: parent.hovered ? 0.3 : -0.4
    contrast: parent.hovered ? 0 : -0.4
    visible: root.enabled && parent.hovered
  }

  Desaturate {
    anchors.fill: root
    source: root
    desaturation: 1.0   // 0.0 = 原色，1.0 = 黑白
    visible: !root.enabled
  }

  W.TapHandler {
    id: mouse
    acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.NoButton
    gesturePolicy: TapHandler.WithinBounds

    onTapped: (p, btn) => {
      if (parent.enabled) {
        if (btn === Qt.LeftButton || btn === Qt.NoButton) {
          parent.clicked();
        } else if (btn === Qt.RightButton) {
          parent.rightClicked();
        }
      }
    }

    onLongPressed: {
      parent.rightClicked();
    }
  }
}